"""
@Time : 2024/6/17 下午9:44
@Auth : AmirKD
@IDE : PyCharm
"""
import tkinter as tk

from modules.Add_Project_Info import Info
from result import result
from util import util
from tkinter import ttk


# 查找项目信息的主方法
def Search_Project_Info(ID, Name, project, creat_time, finish_time, progress):

    # 检查输入是否规范，和阻止空字符串通过
    if len(ID) != 0 and not util().is_ID(ID):
        result().Tip_Add_ID()
        return
    if len(Name) != 0 and util().is_Name(Name):
        result().Tip_Add_name()
        return
    if len(project) != 0 and util().is_project(project):
        result().Tip_Add_project()
        return
    if len(creat_time) != 0 and not util().is_creat_time(creat_time):
        result().Tip_Add_creat_time()
        return
    if len(finish_time) != 0 and not util().is_finish_time(finish_time):
        result().Tip_Add_finish_time()
        return
    if len(progress) != 0 and not util().is_progress(progress):
        result().Tip_Add_progress()
        return

    # 临时列表
    List = []
    # 用来指示是否查找到项目的信息指标
    Flag = False
    # 遍历，根据输入的部分信息找到符合条件的项目
    for i in Info:
        if (i["ID"] == ID or len(ID) == 0) and \
                (i["Name"] == Name or len(Name) == 0) and \
                (i["project"] == project or len(project) == 0) and \
                (i["creat_time"] == creat_time or len(creat_time) == 0) and \
                (i["finish_time"] == finish_time or len(finish_time) == 0) and \
                (i["progress"] == progress or len(progress) == 0) and \
                (len(ID) != 0 or len(Name) != 0 or len(project) != 0 or len(creat_time) != 0 or len(
                    finish_time) != 0 or len(progress) != 0):
            List.append(i)
    if len(List) != 0:
        Flag = True

    # 是否查找成功
    if Flag:
        result().Tip_Search()
    else:
        result().Tip_Search_None()

    # 显示当前项目信息窗口
    if Flag == True:
        # 新建窗口，设置窗口的标题、大小
        window = tk.Toplevel()
        window.title("显示当前项目信息")
        window.geometry('1500x500')

        # 创建表头
        result1 = tk.StringVar()
        result1.set("项目信息如下：")
        Show_result = tk.Label(window, fg="black", bg="white", font=("Microsoft YaHei", 12), bd='0', anchor='nw',
                               textvariable=result1)
        # Show_result.place(x="50", y="50")
        Show_result.pack(side='top', fill='both')

        # 创建表格
        tree = ttk.Treeview(window, columns=("项目序号", "项目名称", "项目内容", "项目创建时间", "项目截止时间", "项目完成进度"),
                            show="headings", displaycolumns="#all")

        # 设置列宽
        tree.column("项目序号", width=100)
        tree.column("项目名称", width=300)
        tree.column("项目内容", width=500)
        tree.column("项目创建时间", width=100)
        tree.column("项目截止时间", width=100)
        tree.column("项目完成进度", width=100)

        # 创建每一列数据的名称
        tree.heading("项目序号", text="项目序号", anchor='w')
        tree.heading("项目名称", text="项目名称", anchor='w')
        tree.heading("项目内容", text="项目内容", anchor='w')
        tree.heading("项目创建时间", text="项目创建时间", anchor='w')
        tree.heading("项目截止时间", text="项目截止时间", anchor='w')
        tree.heading("项目完成进度", text="项目完成进度", anchor='w')

        # 放入数据
        for i in range(0, len(List)):
            tree.insert("", 'end', values=(List[i].get("ID"), List[i].get("Name"),
                                           List[i].get("project"), List[i].get("creat_time"),
                                           List[i].get("finish_time"), List[i].get("progress")))
        # str_out = ''
        # if len(List) != 0:
        #     str_out += "项目信息如下：\n"
        #     # 显示信息标题
        #     str_out += ("{:^7}".format("项目序号") +
        #                 "{:^7}".format("项目名称") +
        #                 "{:^7}".format("项日内容") +
        #                 "{:^7}".format("项目创建时间") +
        #                 "{:^5}".format("项目截止时间") +
        #                 "{:^9}".format("项目完成进度") +
        #                 "\n")
        #     for i in range(0, len(List)):
        #         # 格式化字符串
        #         str_out += ("{:^7}".format(List[i].get("ID")) + ' ' * (
        #                 11 - util().Len_Str(List[i].get("ID"))) +
        #                     "{:^7}".format(List[i].get("Name")) + ' ' * (
        #                             11 - util().Len_Str(List[i].get("Name"))) +
        #                     "{:^7}".format(List[i].get("project")) + ' ' * (
        #                             13 - util().Len_Str(List[i].get("project"))) +
        #                     "{:^7}".format(List[i].get("creat_time")) + ' ' * (
        #                             10 - util().Len_Str(List[i].get("creat_time"))) +
        #                     "{:^5}".format(List[i].get("finish_time")) + ' ' * (
        #                             5 - util().Len_Str(List[i].get("finish_time"))) +
        #                     "{:^9}".format(List[i].get("progress")) + ' ' * (
        #                             11 - util().Len_Str(List[i].get("progress"))) +
        #                     "\n")
        tree.pack(expand=1, fill='both')

        window.mainloop()

    # 在界面打印符合条件项目信息
    # return List
